﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Com.CMS263.PO;
using NHibernate;

namespace Com.CMS263.DAO
{
    public class StudentInfoDAO : BaseService
    {
        public StudentInfoDAO() { }
        public int GetMaxId()
        {
            string hql = "from StudentInfo order by Id desc";
            try
            {
                StudentInfo prod = FindList<StudentInfo>(hql, null, 0, 1).First();
                return prod.Id + 1;
            }
            catch (Exception)
            {

                return 1;
            }
        }
        public IList<StudentInfo> GetListByPager(int page, int pagesize, int listid)
        {
            string hql = " from StudentInfo where 1=1";
            if (listid > 0)
            {
                hql += " and ListId=" + listid;
            }
            hql += " order by ClassName asc, Id Desc";
            try
            {
                if (page == -1)
                {
                    return FindList<StudentInfo>(hql, null, null, null);
                }
                else
                {
                    return FindList<StudentInfo>(hql, null, (page - 1) * pagesize, pagesize);
                }
            }
            catch (Exception)
            {

                return null;
            }

        }
        
        public int GetListByPager(int listid)
        {
            string hql = "select count(*) from StudentInfo where 1=1";
            if (listid > 0)
            {
                hql += " and ListId=" + listid;
            }
            try
            {
                return Convert.ToInt32(GetValue(hql));
            }
            catch (Exception)
            {
                return 0;
            }

        }
        public bool isExist(string userName)
        {
            ISession session = DBHelper.GetISession;
            return session.CreateQuery("from StudentInfo as g  where g.UserName='" + userName + "'").List().Count > 0;
        }


        public StudentInfo Login(string username, string password)
        {
            IList<StudentInfo> list = DBHelper.ExecuteHQL("from StudentInfo where UserName='" + username + "' and UserPass='" + password + "'").List<StudentInfo>();
            if (list.Count > 0)
            {
                return list.First();
            }
            else
            {
                return null;
            }
        }
    }
}
